/* eslint-disable array-callback-return */
import { ms2525c } from "milstd/milstd.esm";
import { ms, std2525c } from "milsymbol/index.esm";
import * as symbolDict from './ms2525cDict.json';
// import * as symbolDict3 from './ms2525cDict3.json'
ms.addIcons(std2525c);
export const ms2525cSchema = {
  WAR: "作战",
  TACGRP: "战术图标",
  SIGINT: "信号与情报",
  STBOPS: "活动",
  EMS: "应急管理",
};

const ms2525dScheam = {
  "01": "飞行器",
  "02": "机载导弹",
  "05": "太空",
  "06": "太空导弹",
  "10": "地面单元",
  "11": "地面平民",
  "15": "地面装备",
  "20": "地面设施",
  "25": "控制措施",
  "30": "海面",
  "35": "水下",
  "36": "地雷战",
  "40": "行动",
  "50": "太空情报",
  "51": "空中情报",
  "52": "地面情报",
  "53": "海面情报",
  "54": "水下情报",
  "60": "网络空间",
};

type schema = typeof ms2525dScheam | typeof ms2525cSchema;
type Ms2525cSchemaType = "WAR" | "TACGRP" | "SIGINT" | "STBOPS" | "EMS";
type Ms2525dSchemaType =
  | "01"
  | "02"
  | "05"
  | "06"
  | "10"
  | "11"
  | "15"
  | "20"
  | "25"
  | "30"
  | "35"
  | "36"
  | "40"
  | "50"
  | "51"
  | "52"
  | "53"
  | "54"
  | "60";
  export type SchemaType = Ms2525cSchemaType;

export const iconSizeConfig = {
  "Team/Crew": 5,
  Squad: 10,
  Section: 15,
  "Platoon/detachment": 20,
  "Company/battery/troop": 25,
  "Battalion/squadron": 30,
  "Regiment/group": 35,
  Brigade: 40,
  Division: 45,
  "Corps/MEF": 50,
  Army: 55,
  "Army Group/front": 60,
  "Region/Theater": 65,
  Command: 70,
};

export const staffComments: string[] = [
  "支援",
  "攻击",
  "巡逻",
  "监视",
  "指挥",
  "侦查",
  "防御",
];

export class Affiliations {
  static get Default(): any {
    return [
      {
        name: "红军",
        key: "H",
      },
      {
        name: "蓝军",
        key: "F",
      },
      // {
      //   name: "盟军",
      //   key: "A",
      // },
      {
        name: "其它",
        key: "U",
      },
      // {
      //   name: "待定",
      //   key: "P",
      // },
    ];
  }
}

export interface SymbolTypeItem{
    name:string;
    fullKey:string;
    key:string
}

export class MS2525CUtil {
  static get War(): SymbolTypeItem[] {
    return [
      {
        name: "空",
        fullKey: "AIRTRK",
        key: "A",
      },
      {
        name: "太空",
        fullKey: "SPC",
        key: "P",
      },
      {
        name: "地面设备",
        fullKey: "GRDTRK_EQT",
        key: "GE",
      },
      {
        name: "地面设施",
        fullKey: "GRDTRK_INS",
        key: "GI",
      },
      {
        name: "地面单元",
        fullKey: "GRDTRK_UNT",
        key: "GU",
      },
      {
        name: "海面",
        fullKey: "SSUF",
        key: "S",
      },
      {
        name: "水下",
        fullKey: "SBSUF",
        key: "U",
      },
      {
        name: "特战",
        fullKey: "SOFUNT",
        key: "F",
      },
    ];
  }
  static get TACGRP(): SymbolTypeItem[] {
    return [
      {
        name: "指控制与机动",
        fullKey: "C2GM",
        key: "C2GM",
      },
      {
        name: "作战勤务保障",
        fullKey: "CSS",
        key: "CSS",
      },
      {
        name: "火力支援",
        fullKey: "FSUPP",
        key: "FSUPP",
      },
      {
        name: "机动性/生存能力",
        fullKey: "MOBSU",
        key: "MOBSU",
      },
      {
        name: "任务",
        fullKey: "TSK",
        key: "TSK",
      },
      {
        name: "其它",
        fullKey: "OTH",
        key: "OTH",
      }
    ];
  }
  static get STBOPS(): SymbolTypeItem[] {
    return [
      {
        name: "非军事团体或组织",
        fullKey: "GRPORG",
        key: "GRPORG",
      },
      {
        name: "个人",
        fullKey: "INDIV",
        key: "INDIV",
      },
      {
        name: "条目",
        fullKey: "ITM",
        key: "ITM",
      },
      {
        name: "位置",
        fullKey: "LOCAT ",
        key: "LOCAT",
      },
      {
        name: "行动",
        fullKey: "OPN",
        key: "OPN",
      },
      {
        name: "掠夺",
        fullKey: "RAPE",
        key: "RAPE",
      },
      {
        name: "暴力活动",
        fullKey: "VIOATY",
        key: "VIOATY",
      }
    ];
  }
  static get SIGINT(): SymbolTypeItem[] {
    return [
      {
        name: "太空情报",
        fullKey: "SPC",
        key: "SPC",
      },
      {
        name: "空情",
        fullKey: "AIRTRK",
        key: "AIRTRK",
      },
      {
        name: "地面情报",
        fullKey: "GRDTRK",
        key: "GRDTRK",
      },
       {
        name: "海面情报",
        fullKey: "SSUF",
        key: "SSUF",
      },
      {
        name: "水下情报",
        fullKey: "SBSUF",
        key: "SBSUF",
      }
    ];
  }
  static get EMS(): SymbolTypeItem[] {
    return [
      {
        name: "暴力事件",
        fullKey: "INCDNT",
        key: "INCDNT",
      },
      {
        name: "基础设施",
        fullKey: "INFSTR",
        key: "INFSTR",
      },
      {
        name: "天灾",
        fullKey: "NATEVT",
        key: "NATEVT",
      },
      {
        name: "活动",
        fullKey: "OPN",
        key: "OPN",
      }
    ];
  }
  static getSymbolTypeArray= (schemaKey:Ms2525cSchemaType):SymbolTypeItem[] => {
      let item:SymbolTypeItem[];  //  "SIGINT" | "STBOPS" | "EMS";
      switch (schemaKey) {
          case 'WAR':
              item = MS2525CUtil.War ;
              break;
          case 'SIGINT':
              item = MS2525CUtil.SIGINT ;
              break;
          case 'TACGRP':
              item = MS2525CUtil.TACGRP ;
              break;
          case 'STBOPS':
              item = MS2525CUtil.STBOPS;
              break;
          case 'EMS':
              item = MS2525CUtil.EMS ;
              break;      
          default:
              item = [] ;
              break;
      }      
      return item;
  }
  static getItem= (schemaKey:Ms2525cSchemaType ,key: string):SymbolTypeItem => {
      let item:SymbolTypeItem;  //  "SIGINT" | "STBOPS" | "EMS";
      switch (schemaKey) {
          case 'WAR':
              item = MS2525CUtil.War.filter(item =>item.key === key)[0] ;
              break;
          case 'SIGINT':
              item = MS2525CUtil.SIGINT.filter(item =>item.key === key)[0] ;
              break;
          case 'TACGRP':
              item = MS2525CUtil.TACGRP.filter(item =>item.key === key)[0] ;
              break;
          case 'STBOPS':
              item = MS2525CUtil.STBOPS.filter(item =>item.key === key)[0] ;
              break;
          case 'EMS':
              item = MS2525CUtil.EMS.filter(item =>item.key === key)[0] ;
              break;      
          default:
              item = MS2525CUtil.EMS.filter(item =>item.key === key)[0] ;
              break;
      }      
      return item;
  }
}
export class SymbolStatus {
  static get Default(): any {
    return [
      {
        name: "无状态",
        key: "-",
      },
      {
        name: "满能",
        key: "C",
      },
      {
        name: "损伤",
        key: "D",
      },
      {
        name: "损坏",
        key: "X",
      },
    ];
  }
}

export type AffiliationKey = "F" | "A" | "H" | "U" | "P";
export type WARKey = "P" | "A" | "GE" | "GI" | "GU" | "S" | "U" | "F";
export type TACGRPKey = "C2GM"| "CSS"| "FSUPP"| "MOBSU"|"TSK"|"OTH";
export type STBOPSKey = "GRPORG"| "INDIV"| "ITM"|"LOCAT"| "OPN"| "RAPE"| "VIOATY";
export type SIGINTKey = "SPC"| "AIRTRK"| "GRDTRK"| "SSUF"|"SBSUF";
export type EMSKey = "INCDNT"| "INFSTR"| "NATEVT"|"OPN";
export type SchemaTypeKey = WARKey |TACGRPKey|STBOPSKey|SIGINTKey|EMSKey;
export type StatusKey = "C" | "D" | "X";

const buildSIDCFrom2525c = (affiliation:AffiliationKey,schemaType:Ms2525cSchemaType, schemaKey:SchemaTypeKey, mainIcon) =>{
    let sidc = `${ mainIcon['code scheme']}${affiliation}${mainIcon['battle dimension']}-${mainIcon['code']}` ;
    const fullKey = MS2525CUtil.getItem(schemaType,schemaKey).fullKey;
    if (fullKey === 'GRDTRK_EQT') sidc += 'E-----';
    if (fullKey === 'GRDTRK_INS') sidc += '------H-';
    // @ts-ignore
    // sym.name = mainIcon.name[mainIcon.name.length -1];
    return sidc;
}

const getSymbolNameZh = sidc =>{
  // @ts-ignore
  const symbols = symbolDict.default.filter(item => item.sidc === `${sidc.substring(0,1)}F${sidc.substring(2)}`);
  return symbols[0].nameZH;
}

export const getSymbolBySidc = (sidc: string): string => {
  const symbol = new ms.Symbol(sidc, { size: 32 });
  let url;
  try {
    url = symbol.toDataURL();
  } catch (Error) {
    url =
      "data:image/svg+xml;base64," +
      window.btoa(unescape(encodeURIComponent(symbol.asSVG())));
  }
  return url;
};

// const transSymbolNameZh = () =>{
//   // @ts-ignore
//   symbolDict.default.map(item => {
//        item.svgDataURL = getSymbolBySidc(item.sidc);    
//   });
//   // @ts-ignore
//   console.log(JSON.stringify(symbolDict.default));
// }

// transSymbolNameZh();

export const searchSymbols = (searchKeyword:string,whthStatus:boolean = true) =>{
  // @ts-ignore
  const symbolSet = symbolDict.default.filter(item => (item.names && item.names.join('').indexOf(searchKeyword.toUpperCase()) >= 0 ) || item.nameZH.indexOf(searchKeyword) >= 0);  
  let mainIcons = JSON.parse(JSON.stringify(symbolSet));
  ['H','U'].map((item:string) => {
    symbolSet.map(mainIcon => mainIcon.sidc =`${mainIcon.sidc.substring(0,1)}${item}${mainIcon.sidc.substring(2)}` );
    const symbolSetClone = JSON.parse(JSON.stringify(symbolSet));
    mainIcons = mainIcons.concat(symbolSetClone );
  });
  let mainIconsWithStatus = JSON.parse(JSON.stringify(mainIcons));
  if(whthStatus){
    SymbolStatus.Default.map(item =>{
      mainIcons.map(mainIcon => mainIcon.sidc =`${mainIcon.sidc.substring(0,3)}${item.key}${mainIcon.sidc.substring(4)}` );
      const mainIconsClone = JSON.parse(JSON.stringify(mainIcons));
      mainIconsWithStatus = mainIconsWithStatus.concat(mainIconsClone);
    })
  }
  // @ts-ignore
  mainIconsWithStatus.map((mainIcon): void => {
    const sidc =mainIcon.sidc;
    mainIcon.label = mainIcon.nameZH ; // mainIcon.name[mainIcon.name.length -1] ;
    mainIcon.sidc = sidc ; //`${sidc.substring(0,3)}${symbolStatus}${sidc.substring(4)}`;
    mainIcon.subType = mainIcon.name[mainIcon.name.length -2] || mainIcon.label;
    mainIcon.type = mainIcon.name[mainIcon.name.length -3] || mainIcon.subType || mainIcon.label ;
    const symbolItem = new ms.Symbol( mainIcon.sidc );
    mainIcon.isValid = symbolItem.isValid();
    if(mainIcon.isValid){
      symbolItem.setOptions({infoColor: symbolItem.getColors().fillColor[symbolItem.getMetadata().affiliation]});
      mainIcon.pngDataURL = symbolItem.asCanvas(window.devicePixelRatio).toDataURL();
      try{
        mainIcon.svgDataURL = symbolItem.toDataURL();
      }
      catch (Error) {
        mainIcon.svgDataURL = "data:image/svg+xml;base64," + window.btoa(unescape(encodeURIComponent(symbolItem.asSVG())))
      }
    }
  })  
  mainIconsWithStatus = mainIconsWithStatus.filter(item =>item.isValid);
  return mainIconsWithStatus;

}
// console.log(searchSymbols('sensor',true));
export const searchMilSymbolSet = (
  affiliation: AffiliationKey,
  schemaType: SchemaType,
  schemaKey: SchemaTypeKey,
  searchKeyword?: string
) => {
    const standardJSON =ms2525c;
    let mainIcons;
    try {
      mainIcons=standardJSON[schemaType][MS2525CUtil.getItem(schemaType, schemaKey).fullKey]['main icon'];    
    } catch (e) {
      console.log(schemaType, schemaKey,MS2525CUtil.getItem(schemaType, schemaKey),standardJSON[schemaType])
    }
    if(searchKeyword && searchKeyword.length>0){
        mainIcons = mainIcons.filter(mainIcon =>mainIcon.name.join('').indexOf(searchKeyword) >=0);
    }
    mainIcons.map(mainIcon => {
        mainIcon.sidc = buildSIDCFrom2525c(affiliation,schemaType,schemaKey,mainIcon);
        mainIcon.label = mainIcon.name[mainIcon.name.length -1] ;
        mainIcon.subType = mainIcon.name[mainIcon.name.length -2] || mainIcon.label;
        mainIcon.type = mainIcon.name[mainIcon.name.length -3] || mainIcon.subType || mainIcon.label ;
    });
   
    return mainIcons;
};

export const searchMilSymbolSetIcons = (
  symbolStatus:StatusKey,
  affiliation: AffiliationKey,
  schemaType: SchemaType,
  schemaKey: SchemaTypeKey,
  searchKeyword: string ='',
  symbolOptions:object={size:32}
) => {
    let mainIcons=searchMilSymbolSet(affiliation,schemaType,schemaKey,searchKeyword);
    mainIcons.map((mainIcon): void => {
        const sidc = buildSIDCFrom2525c(affiliation,schemaType,schemaKey,mainIcon);
        mainIcon.label = getSymbolNameZh(sidc) ; // mainIcon.name[mainIcon.name.length -1] ;
        mainIcon.sidc = `${sidc.substring(0,3)}${symbolStatus}${sidc.substring(4)}`;
        mainIcon.subType = mainIcon.name[mainIcon.name.length -2] || mainIcon.label;
        mainIcon.type = mainIcon.name[mainIcon.name.length -3] || mainIcon.subType || mainIcon.label ;
        const symbolItem = new ms.Symbol( mainIcon.sidc, symbolOptions);
        mainIcon.isValid = symbolItem.isValid();
        if(mainIcon.isValid){
          symbolItem.setOptions({infoColor: symbolItem.getColors().fillColor[symbolItem.getMetadata().affiliation]});
          mainIcon.pngDataURL = symbolItem.asCanvas(window.devicePixelRatio).toDataURL();
          try{
            mainIcon.svgDataURL = symbolItem.toDataURL();
          }
          catch (Error) {
            mainIcon.svgDataURL = "data:image/svg+xml;base64," + window.btoa(unescape(encodeURIComponent(symbolItem.asSVG())))
          }
        }
    })
    mainIcons = mainIcons.filter(item =>item.isValid);
    // console.log(mainIcons);
    return mainIcons;
};

  // @ts-ignore
  // symbolDict.default.map(item => {
  //   item.nameZH =item.name
  // });
  // @ts-ignore
  // console.log(JSON.stringify(symbolDict.default));


  // const thisStd=[];
    // // tslint:disable-next-line: forin
    // for (const i in standardJSON) {
    //     // console.log(i);
    //     // tslint:disable-next-line: forin
    //     for (const j in standardJSON[i]) {
    //     console.log(standardJSON[i][j]);
    //     if (typeof standardJSON[i][j] === 'object') {
    //         const mainIcons = standardJSON[i][j]['main icon'];
    //         mainIcons.map(mainIcon =>{
    //         const sym ={};
    //         // @ts-ignore
    //         // tslint:disable-next-line: no-string-literal
    //         sym.sidc = mainIcon['code scheme'] +     'F' + mainIcon['battle dimension'] + '-' + mainIcon['code'];
    //             // @ts-ignore
    //         if (j === 'GRDTRK_EQT') sym.sidc += 'E-----';
    //         // @ts-ignore
    //         if (j === 'GRDTRK_INS') sym.sidc += '------' + 'H-';
    //         // @ts-ignore
    //         sym.name = mainIcon.name[mainIcon.name.length -1];
    //         // @ts-ignore
    //         thisStd.push(sym);
    //         });
    //     }
    //     }
    // }
    // console.log(JSON.stringify(thisStd));
    // console.log(schemaType, schemaKey,MS2525CUtil.getItem(schemaType, schemaKey).fullKey,standardJSON[schemaType])
    
